import Mock from 'mockjs'

// 生成角色列表数据
const generateRoles = () => {
  const roles = [
    {
      id: 1,
      name: '超级管理员',
      code: 'SUPER_ADMIN',
      description: '系统超级管理员，拥有所有权限',
      status: '1',
      createTime: '2024-01-01 00:00:00'
    },
    {
      id: 2,
      name: '普通用户',
      code: 'USER',
      description: '普通用户，拥有基本权限',
      status: '1',
      createTime: '2024-01-02 00:00:00'
    },
    {
      id: 3,
      name: '访客',
      code: 'GUEST',
      description: '访客用户，只有查看权限',
      status: '1',
      createTime: '2024-01-03 00:00:00'
    }
  ]
  
  // 生成更多角色数据
  for (let i = 4; i <= 20; i++) {
    roles.push({
      id: i,
      name: Mock.Random.cword(2, 4) + '角色',
      code: Mock.Random.string('upper', 3, 8),
      description: Mock.Random.csentence(10, 30),
      status: Mock.Random.pick(['1', '0']),
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    })
  }
  
  return roles
}

let roleList = generateRoles()

export default function() {
  // 获取角色列表
  Mock.mock(/\/api\/role\/list/, 'get', (options) => {
    const url = new URL(options.url, 'http://localhost')
    const page = parseInt(url.searchParams.get('page')) || 1
    const pageSize = parseInt(url.searchParams.get('pageSize')) || 10
    const name = url.searchParams.get('name') || ''
    const status = url.searchParams.get('status') || ''
    
    let filteredRoles = [...roleList]
    
    // 按角色名称筛选
    if (name) {
      filteredRoles = filteredRoles.filter(role => 
        role.name.includes(name)
      )
    }
    
    // 按状态筛选
    if (status) {
      filteredRoles = filteredRoles.filter(role => 
        role.status === status
      )
    }
    
    const total = filteredRoles.length
    const start = (page - 1) * pageSize
    const end = start + pageSize
    const list = filteredRoles.slice(start, end)
    
    return {
      code: 200,
      message: '获取成功',
      data: {
        list,
        total,
        page,
        pageSize
      }
    }
  })

  // 创建角色
  Mock.mock('/api/role/create', 'post', (options) => {
    const roleData = JSON.parse(options.body)
    const newRole = {
      id: roleList.length + 1,
      ...roleData,
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    }
    roleList.push(newRole)
    
    return {
      code: 200,
      message: '创建成功',
      data: newRole
    }
  })

  // 更新角色
  Mock.mock(/\/api\/role\/update\/\d+/, 'put', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    const updateData = JSON.parse(options.body)
    
    const index = roleList.findIndex(role => role.id === id)
    if (index !== -1) {
      roleList[index] = { ...roleList[index], ...updateData }
      return {
        code: 200,
        message: '更新成功',
        data: roleList[index]
      }
    } else {
      return {
        code: 404,
        message: '角色不存在',
        data: null
      }
    }
  })

  // 删除角色
  Mock.mock(/\/api\/role\/delete\/\d+/, 'delete', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    
    const index = roleList.findIndex(role => role.id === id)
    if (index !== -1) {
      roleList.splice(index, 1)
      return {
        code: 200,
        message: '删除成功',
        data: null
      }
    } else {
      return {
        code: 404,
        message: '角色不存在',
        data: null
      }
    }
  })

  // 获取角色详情
  Mock.mock(/\/api\/role\/detail\/\d+/, 'get', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    
    const role = roleList.find(role => role.id === id)
    if (role) {
      return {
        code: 200,
        message: '获取成功',
        data: role
      }
    } else {
      return {
        code: 404,
        message: '角色不存在',
        data: null
      }
    }
  })
} 